package com.haofengsoft.scce.ui.consultation;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.haofengsoft.platform.companent.listview.DragSortListView;
import com.haofengsoft.scce.R;
import com.haofengsoft.scce.adapter.information.ManageAttentionAdapter;
import com.haofengsoft.scce.controller.AttentionController;
import com.haofengsoft.scce.db.bean.AttentionItem;

public class ManageAttentionActivity extends Activity {

    private DragSortListView addAttentionDslv;
    private ManageAttentionAdapter addAttentionAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.manage_attention);
        addAttentionDslv = (DragSortListView) findViewById(R.id.add_attention_dsLv);

        TextView changeOverTv = (TextView) findViewById(R.id.add_attention_tv);
        changeOverTv.setOnClickListener(new AttentionOverClickListener());
        initAttentionData();
    }

    class AddAttentionDropListener implements DragSortListView.DropListener {

        @Override
        public void drop(int from, int to) {
            AttentionItem item = addAttentionAdapter.getItem(from);
            addAttentionAdapter.handleItemMove(from, to);//位置特别重要，先处理排序，后处理位置
            addAttentionAdapter.remove(item);
            addAttentionAdapter.insert(item, to);
            addAttentionDslv.moveCheckState(from, to);
        }
    }

    class AttentionOverClickListener implements OnClickListener {

        @Override
        public void onClick(View view) {
            //保存
            AttentionController.getInstance(ManageAttentionActivity.this).insertAttentionList(addAttentionAdapter.getModelList());
            finish();
        }

    }

    private void initAttentionData() {
        List<AttentionItem> itemList = AttentionController.getInstance(ManageAttentionActivity.this).gainAttentionAll();
        if (itemList.size() == 0) {
//            //表示没有存库
//            自选  OptionalStampFragment
//            要闻  FocusNewsFragment
//            直播  LiveFragment
//            涨停板 DailyLimitBoardFragment
//            点石成金 StoneGoldFragment
//            行情综述 MarketOverviewFragment
//            币卡市评 StampCommentFragment
//            嘉宾专栏 GuestColumnFragment
//            南京   StampNanJingFragment
//            中南   StampZhongNanFragment
//            上海   StampShangHaiFragment
//            北京   StampBeijingFragment
//            投资锦囊 InvestmentTipsFragment
            AttentionItem itemOptional = new AttentionItem(Long.valueOf(AttentionController.OPTIONAL_STAMP), AttentionController.OPTIONAL_STAMP, 0, "自选", 0, "汇聚自选股的相关新闻");
            itemList.add(itemOptional);

            AttentionItem itemFocus = new AttentionItem(Long.valueOf(AttentionController.FOCUS_NEWS), AttentionController.FOCUS_NEWS, 1, "要闻", 0, "7-24小时国内和国际新闻播报");
            itemList.add(itemFocus);

            AttentionItem itemLive = new AttentionItem(Long.valueOf(AttentionController.LIVE), AttentionController.LIVE, 2, "直播", 0, "7-24小时国内和国际新闻直播");
            itemList.add(itemLive);
            AttentionItem itemDaily = new AttentionItem(Long.valueOf(AttentionController.DAILY_LIMIT_BOARD), AttentionController.DAILY_LIMIT_BOARD, 3, "涨停板", 0, "今日牛逼股");
            itemList.add(itemDaily);
            AttentionItem itemGold = new AttentionItem(Long.valueOf(AttentionController.STONE_GOLD), AttentionController.FOCUS_NEWS, 4, "点石成金", 0, "短线黑马");
            itemList.add(itemGold);
            AttentionItem itemMarket = new AttentionItem(Long.valueOf(AttentionController.MARKET_OVERVIEW), AttentionController.MARKET_OVERVIEW, 5, "行情综述", 0, "7*24小时国内和国际新闻播报");
            itemList.add(itemMarket);
            AttentionItem itemComment = new AttentionItem(Long.valueOf(AttentionController.STAMP_COMMENT), AttentionController.STAMP_COMMENT, 6, "币卡市评", 0, "7*24小时国内和国际新闻播报");
            itemList.add(itemComment);

            AttentionItem itemGuest = new AttentionItem(Long.valueOf(AttentionController.GUEST_COLUMN), AttentionController.GUEST_COLUMN, 7, "嘉宾专栏", 0, "7*24小时国内和国际新闻播报");
            itemList.add(itemGuest);
            AttentionItem itemNanjing = new AttentionItem(Long.valueOf(AttentionController.STAMP_NANJING), AttentionController.STAMP_NANJING, 8, "南京", 0, "7*24小时国内和国际新闻播报");
            itemList.add(itemNanjing);

            AttentionItem itemZhongNan = new AttentionItem(Long.valueOf(AttentionController.STAMP_ZHONGNAN), AttentionController.STAMP_ZHONGNAN, 9, "中南", 0, "7*24小时国内和国际新闻播报");
            itemList.add(itemZhongNan);
            AttentionItem itemShanghai = new AttentionItem(Long.valueOf(AttentionController.STAMP_SHANGHAI), AttentionController.STAMP_SHANGHAI, 10, "上海", 0, "7*24小时国内和国际新闻播报");
            itemList.add(itemShanghai);
            AttentionItem itemBeijing = new AttentionItem(Long.valueOf(AttentionController.STAMP_BEIJING), AttentionController.STAMP_BEIJING, 11, "北京", 0, "7*24小时国内和国际新闻播报");
            itemList.add(itemBeijing);
            AttentionItem itemTips = new AttentionItem(Long.valueOf(AttentionController.INVESTMENT_TIPS), AttentionController.INVESTMENT_TIPS, 12, "投资锦囊", 0, "7*24小时国内和国际新闻播报");
            itemList.add(itemTips);
            System.out.println("存入数据" + JSON.toJSONString(itemList));
            AttentionController.getInstance(ManageAttentionActivity.this).insertAttentionList(itemList);
        }
        //
        addAttentionAdapter = new ManageAttentionAdapter(
                ManageAttentionActivity.this, R.layout.add_attention_item,
                itemList);
        addAttentionDslv.setAdapter(addAttentionAdapter);
        addAttentionDslv.setDropListener(new AddAttentionDropListener());
        addAttentionDslv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    }


}
